之前把电脑恢复出厂设置了所以重新配了环境。这次正好把上次的问题解决了,记录下基于WSL2的开发环境配置。包括WSL2, sagemath, vscode, rust, C/C++.
我vim等工具玩的也不多,而且感觉也没需求(不过这次重装其实就是打N1CTF半截,vscode连不上WSL2,然后WSL2也炸了)。所以不搞花里胡哨的,一切从简..
但是sagemath 10.x版本对我来说很有必要,而apt包管理器目前只能下到9.5,所以sagemath是自己编译的。
前置
WSL2 + vscode
安装
默认是装C盘的,上次我C盘空间不足,很折磨。 所以这次选择手动安装。
正确做法是import或者先官方安装 export再import,千万别自己解压缩包然后像下面这样安装!!
- 先升级Windows11到23H2,否则不方便让WSL2科学上网。这会影响到sagemath高版本的编译。23H2应该很快就发布了,急着用就看这篇的2023.9.19更新 
- 下载WSL2并做安装所需的前置步骤:下载发行版里面选个ubuntu的LTS高版本下载,同时往上翻完成前五步 
- 下载的 - .appx文件后缀改为- .zip,架构的- .appx包拖出来再改为- .zip,解压到你想安装的文件夹。然后点击ubuntu.exe
- 直接装vscode,打开右下角自动提示安装WSL2扩展。下面改xxx文件就可以直接code xxx了。没写权限先chmod,再改回去 
- Windows上装点东西:设置 WSL 开发环境的最佳做法分步指南。 Windows Terminal 肯定还是要装,别的看着办。 
ubuntu配置
- 换国内源 
- 安装实用工具(可选):thefuck, ag, … 
- 给WSL2加内存、处理器核心数等等。根据官方文档示例,新建或编辑WSL的全局配置文件内容如下(根据自己电脑的配置自行修改),文件路径为 - C:\Users\<YourUserName>\.wslconfig- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43- # Settings apply across all Linux distros running on WSL 2 
 [wsl2]
 # Limits VM memory to use no more than 14 GB, this can be set as whole numbers using GB or MB
 memory=14GB
 # Sets the VM to use six virtual processors
 processors=6
 # Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
 # kernel=C:\\temp\\myCustomKernel
 # Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
 # kernelCommandLine = vsyscall=emulate
 # Sets amount of swap storage space to 14GB, default is 25% of available RAM
 swap=14GB
 # Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
 swapfile=E:\\temp\\wsl-swap.vhdx
 # Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
 pageReporting=false
 # Turn off default connection to bind WSL 2 localhost to Windows localhost
 localhostforwarding=true
 # Disables nested virtualization
 nestedVirtualization=false
 # Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
 debugConsole=true
 # Enable experimental features
 [experimental]
 sparseVhd=true
 # For network purpose. Only useful when windows version is 23H2, and wsl --update --pre-release
 # Currently only a windows insider of Release Preview Channel can use this..
 networkingMode=mirrored
 dnsTunneling=true
 firewall=true
 autoProxy=true- 其中 - swapfile设定的路径所在文件夹必须存在。编辑之后,让WSL2关机再重启才会生效。Powershell- wsl --shutdown关机
- 让WSL2能科学上网,eg. clash. 评论区也写了怎么一键配置和取消配置。
Rust
官网或直接一句话安装:
| 1 | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh | 
vscode扩展插件装上rust-analyzer。
C/C++
需要读一些项目代码,既安装了gcc又安装了clang。
目前vscode配上了扩展: call graph(LuoZhihao写的那个)。感觉效果差一些,准备摸索一下用doxygen + codeviz生成call graph。
conda+sage源码安装sage
自己编译sagemath真的很坑,很难装上,基本上得去google group请教sagemath的人。但是用anaconda安装还是不难的。
此处参考sagemath conda安装方法的’Using conda to provide all dependencies for the Sage library’章节
- 先把sage源码 git clone下来, checkout 到 master分支:1 
 2
 3
 4
 5ORIG=https://github.com/sagemath/sage.git 
 # git clone -c core.symlinks=true --branch develop --tags $ORIG
 git clone $ORIG
 cd sage
 git checkout master
- 找个合适地方下载conda。注意读LICENSE环节,然后全部选择yes1 
 2curl -L -O https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh 
 sh Mambaforge-$(uname)-$(uname -m).sh
- 配置conda源为阿里源,据说清华源中科大源现在不好用了:配置说明
- conda install mamba
- 配置mamba源,类似conda源的配置:说明. 实际上直接把 .condarc 的拷一下就行. 然后也是 mamba clean -i
- 回到clone的sage文件夹,然后开始安装环境1 
 2
 3# 可以把sage改成XXX,以后activate时 mamba activate XXX 
 mamba env create --file src/environment-3.11.yml --name sage
 mamba activate sage
- 1 
 2
 3- ./bootstrap 
 pip install --no-build-isolation -v -v --editable ./pkgs/sage-conf_conda ./pkgs/sage-setup
 pip install --no-build-isolation --config-settings editable_mode=compat -v -v --editable ./src
- 验证sagemath已安装1 sage -c 'print(version())' 
